<?php
// +----------------------------------------------------------------------
// | LanCMS [ 永久网络工作室 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2019 http://www.19www.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Lajox <lajox@19www.com>
// +----------------------------------------------------------------------

namespace app\common\exception;

use \Exception;
use think\exception\Handle;
use think\facade\App;
use \think\Response;
use app\common\common\Helper;
use app\common\constant\Constants;

class ExceptionHandler extends Handle {

    public function render(Exception $e)
    {
        if (App::isDebug()) {
            // 请求异常
            if ($e instanceof Exception) {
                $data = [
                    'name'    => get_class($e),
                    'file'    => $e->getFile(),
                    'line'    => $e->getLine(),
                    'message' => $this->getMessage($e),
                    'trace'   => $e->getTrace(),
                    'code'    => $this->getCode($e),
                    'source'  => $this->getSourceCode($e),
                    'datas'   => $this->getExtendData($e),
                ];
                $request_uri = request()->server('REQUEST_URI');
                $ext = pathinfo($request_uri, PATHINFO_EXTENSION);
                if(strpos($data['message'], '应用模块不存在') !== false && in_array(strtolower($ext), ['css','js','jpg','jpeg','png','gif','ico','txt'])) {
                    $data = [
                        'name'    => get_class($e),
                        'message' => '访问的资源不存在',
                    ];
                }
                return $this->jsonResult($data);
            }
        } else {
            if ($e instanceof Exception) {
                $data = [
                    'name'    => get_class($e),
                    'message' => $this->getMessage($e),
                ];
                return $this->jsonResult($data);
            }
        }

        //TODO::开发者对异常的操作
        //可以在此交由系统处理
        return parent::render($e);
    }

    private function jsonResult($data)
    {
        //return Helper::jsonError($data['message'], $code = Constants::JSON_STATUS_ERROR, $data);
        if(strpos($data['name'], 'PDOException') === false || strpos($data['message'], 'SQLSTATE[HY000] [1049] Unknown database') === false) {
            return Helper::jsonError($data['message'], $code = Constants::JSON_STATUS_ERROR, $data);
        } else {
            $result = ['code' => Constants::JSON_STATUS_ERROR, 'msg' => $data['message'], 'data' => $data];
            $response = Response::create($result, 'json');
            if(strpos($data['message'], 'SQLSTATE[HY000] [1049] Unknown database') === false) {
                model('common/Apilog')->record($response);
            }
            //# $response->send();
            $contentType = $response->getHeader('Content-Type') ? $response->getHeader('Content-Type') : 'application/json; charset=utf-8';
            header('Content-Type: '. $contentType);
            echo json_encode($result, JSON_UNESCAPED_UNICODE);
            exit;
        }
    }

}
